package org.spring.blog.service.impl;

import org.spring.blog.dao.LooperDao;
import org.spring.blog.pojo.Looper;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.ILooperService;
import org.spring.blog.utils.LRConstants;
import org.spring.blog.utils.SnowflakeIdWorker;
import org.spring.blog.utils.TextUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LooperService implements ILooperService {

    final
    SnowflakeIdWorker snowflakeIdWorker;

    final
    LooperDao looperDao;

    public LooperService(SnowflakeIdWorker snowflakeIdWorker, LooperDao looperDao) {
        this.snowflakeIdWorker = snowflakeIdWorker;
        this.looperDao = looperDao;
    }

    /**
     * 添加轮播图
     *
     * @param _looper 轮播图
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult insert(Looper _looper) {
        // check data
        if (TextUtil.isEmpty(_looper.getTitle())) {
            return ResponseResult.failed(ResponseState.looper_title_cant_empty_message, ResponseState.looper_title_cant_empty_code);
        }

        if (TextUtil.isEmpty(_looper.getImageUrl())) {
            return ResponseResult.failed(ResponseState.looper_image_url_cant_empty_message, ResponseState.looper_image_url_cant_empty_code);
        }

        if (TextUtil.isEmpty(_looper.getTargetUrl())) {
            return ResponseResult.failed(ResponseState.looper_target_url_cant_empty_message, ResponseState.looper_target_url_cant_empty_code);
        }

        // complete data
        _looper.setId(String.valueOf(snowflakeIdWorker.nextId()))
                .setState(LRConstants.db_publish_status)
                .setUpdateTime(new Date())
                .setCreateTime(new Date())
                .setOrder(LRConstants.db_default_order_value);

        //save data
        looperDao.save(_looper);

        return ResponseResult.success();
    }

    /**
     * 获取looper 列表
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult list() {
        // check data
        // complete date
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");

        List<Looper> all = looperDao.findAll(new Specification<Looper>() {
            private static final long serialVersionUID = -4742590515986005102L;

            @Override
            public Predicate toPredicate(Root<Looper> _root, CriteriaQuery<?> _criteriaQuery, CriteriaBuilder _criteriaBuilder) {
                return _criteriaBuilder.equal(_root.get("state").as(String.class), LRConstants.db_publish_status);
            }
        }, sort);

        return ResponseResult.success().setData(all);
    }

    /**
     * 获取looper 列表
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult listForAdmin() {
        // check data
        // complete date
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");

        List<Looper> all = looperDao.findAll(sort);

        return ResponseResult.success().setData(all);
    }

    /**
     * 查询looper 对象
     *
     * @param _id 唯一id
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult query(String _id) {

        Looper oneById = looperDao.findOneById(_id);

        if (oneById == null) {
            return ResponseResult.failed(ResponseState.data_not_have_message, ResponseState.data_not_have_code);
        }
        return ResponseResult.success().setData(oneById);
    }

    /**
     * 更新 looper 对象
     *
     * @param _id     唯一id
     * @param _looper looper 实体对象
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult update(String _id, Looper _looper) {
        // check data
        Looper oneById = looperDao.findOneById(_id);

        if (oneById == null) {
            return ResponseResult.failed(ResponseState.data_not_have_message, ResponseState.data_not_have_code);
        }

        // complete data
        if (!TextUtil.isEmpty(_looper.getImageUrl())) {
            oneById.setImageUrl(_looper.getImageUrl());
        }

        if (!TextUtil.isEmpty(_looper.getTargetUrl())) {
            oneById.setTargetUrl(_looper.getTargetUrl());
        }

        if (!TextUtil.isEmpty(_looper.getTitle())) {
            oneById.setTitle(_looper.getTitle());
        }

        // 在前端可以补齐一下order
        oneById.setUpdateTime(new Date())
                .setOrder(_looper.getOrder())
                .setState(_looper.getState());

        looperDao.save(oneById);
        return ResponseResult.success();
    }

    /**
     * 删除 looper 对象
     *
     * @param _id 唯一id
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult delete(String _id) {

        looperDao.deleteById(_id);

        return ResponseResult.success();
    }
}
